/** 
 * @Title: PriceDao.java 
 * @Package com.zizo.autohome.word.lanniao.price 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月7日 下午5:29:30 
 */
package com.zizo.autohome.word.flowtrend.price;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: PriceDao
 * @Description: TODO 官方指导价*70%<=裸车价<=官方指导价*120%
 * @author HeYan
 * @date 2016年9月7日 下午5:29:30
 * 
 */
public class PriceDao {
	private Connection conn = ConnectionPool.getConnection("autohome", "localhost", "3306");

	/**
	 * @Title: exchange
	 * @Description: TODO 将car_model_info.autohome_core_basicparam 中的
	 *               bp_factory_price 去掉万字 添加到 price.autohome_price
	 *               中的price_gfzdj
	 * @return void
	 * @author Alexis
	 */
	public final void exchange() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bp_modelid,bp_factory_price FROM car_model_info.autohome_core_basicparam";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String temp = rs.getString("bp_factory_price");
				if (temp.contains("暂无报价")) {
					continue;
				}
				int model_id = rs.getInt("bp_modelid");
				double price = Double.parseDouble(rs.getString("bp_factory_price").split("万")[0]);
				getPrice(model_id, price);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public final void getPrice(int model_id, double zdj) {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT price_id,price_lcj FROM price.autohome_price WHERE price_model_id=" + model_id;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int price_id = rs.getInt("price_id");
				double lcj = rs.getDouble("price_lcj");
				int istrue = 0;
				if (lcj <= zdj * 1.2 && lcj >= zdj * 0.7) {
					istrue = 1;
				}
				updatePrice(price_id, zdj, istrue);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public final void updatePrice(int price_id, double zdj, int istrue) {
		PreparedStatement ps = null;
		String sql = "UPDATE price.autohome_price SET price_gfzdj=?,price_istrue=? WHERE price_id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setDouble(1, zdj);
			ps.setInt(2, istrue);
			ps.setInt(3, price_id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		PriceDao dao = new PriceDao();
		dao.exchange();
	}
}
